
    var deleteArticleId="";
    $('.userList .clickLi').click(function () {
        var flag = $(this).attr('class').substring(8);
        $('#personalDate,#basicSetting,#leaveMessage,#privateWord').css("display","none");
        $("#" + flag).css("display","block");
    });

    $('.basicSetting').click(function () {
        $('#phone').val("");
        $('#authCode').val("");
        $('#password').val("");
        $('#surePassword').val("");
    });

    // 失败消息盒
    function dangerNotice(notice) {
        $('.dangerNotice').html(notice);
        $('.dangerNoticeAlert').css("display","block");
        var closeNoticeBox = setTimeout(function () {
            $('.dangerNoticeAlert').css("display","none");
        },3000);
    }
    // 成功消息盒
    function successNotice(notice) {
        $('.successNotice').html(notice);
        $('.successNoticeAlert').css("display","block");
        var closeNoticeBox = setTimeout(function () {
            $('.successNoticeAlert').css("display","none");
        },3000);
    }

    //更改头像
    function imgChange(e) {
        var dom =$("input[id^='imgTest']")[0];
        var reader = new FileReader();
        reader.onload = (function (file) {
            return function (e) {
                $.ajax({
                    type:'POST',
                    url:'/uploadHead',
                    dataType:'json',
                    data:{
                        img:this.result
                    },
                    success:function (data) {
                        if(data['status'] == 403){
                            $.get("/toLogin",function(data,status,xhr){
                                window.location.replace("/login");
                            });
                        } else {
                            if(data['status'] == 200){
                                $('#headPortrait').attr("src",data['avatarImgUrl']);
                                successNotice("更改头像成功");
                            } else {
                                dangerNotice("更改头像失败")
                            }
                        }

                    },
                    error:function () {
                    }
                });
            };
        })(e.target.files[0]);
        reader.readAsDataURL(e.target.files[0]);
    }

    //获得个人信息
    function getUserPersonalInfo() {
        $.ajax({
            type:'get',
            url:'/getUserPersonalInfo',
            dataType:'json',
            data:{
            },
            success:function (data) {
                if(data['status'] == 403){
                    $.get("/toLogin",function(data,status,xhr){
                        window.location.replace("/login");
                    });
                } else {
                    $('#username').attr("value",data['result']['username']);
                    $('#userphone').attr("value",data['result']['phone']);
                    $('#trueName').attr("value",data['result']['trueName']);
                    $('#birthday').attr("value",data['result']['birthday']);
                    var gender = data['result']['gender'];
                    if(gender == "男"){
                        $('.genderTable input').eq(0).attr("checked","checked");
                    } else {
                        $('.genderTable input').eq(1).attr("checked","checked");
                    }
                    $('#email').attr("value",data['result']['email']);
                    $('#personalBrief').val(data['result']['personalBrief']);
                }
            },
            error:function () {
            }
        });
    }

    //获得头像url
    function showHeadPortrait() {
        $.ajax({
            type:'get',
            url:'/getHeadPortraitUrl',
            dataType:'',
            data:{
            },
            success:function (data) {
                $('#headPortrait').attr("src",data);
            },
            error:function () {
            }
        });
    }

    //保存个人资料
    var savePersonalDateBtn = $('#savePersonalDateBtn');
    var nickname = $('#username');
    var username0 = $('#trueName');
    var birthday = $('#birthday');
    var sex = $('.genderTable input');
    var email = $('#email');
    var signature = $('#personalBrief');
    savePersonalDateBtn.click(function () {
        var nickname1 = nickname.val();
        var sexValue = "男";
        if(nickname1.length === 0){
            dangerNotice("昵称不能为空");
        } else if(!sex[0].checked && !sex[1].checked){
            dangerNotice("性别不能为空");
        } else {
            if(sex[0].checked){
                sexValue = "男";
            } else {
                sexValue = "女";
            }
            $.ajax({
                type:'post',
                url:'/savePersonalDate',
                dataType:'json',
                data:{
                    nickName:nickname.val(),
                    username0:username0.val(),
                    birthday:birthday.val(),
                    sex:sexValue,
                    email:email.val(),
                    signature:signature.val()
                },
                success:function (data) {
                    if (data['status'] == 200){
                        successNotice("更改个人信息成功");
                    } else {
                        dangerNotice("更改个人信息失败");
                    }
                },
                error:function () {
                }
            });
        }
    });

    var phone = $('#phone');
    var authCode = $('#authCode');
    var password = $('#password');
    var surePassword = $('#surePassword');

    phone.blur(function () {
        var pattren = /^1[345789]\d{9}$/;
        var phoneValue = phone.val();
        if(pattren.test(phoneValue)){
            phone.removeClass("wrong");
            phone.addClass("right");
        } else {
            phone.removeClass("right");
            phone.addClass("wrong");
        }
    });
    phone.focus(function () {
        $('.notice').css("display","none");
    });

    // 定义发送时间间隔(s)
    var my_interval;
    my_interval = 60;
    var timeLeft = my_interval;
    //重新发送计时函数
    var timeCount = function() {
        window.setTimeout(function() {
            if(timeLeft > 0) {
                timeLeft -= 1;
                $('#authCodeBtn').html(timeLeft + "秒重新发送");
                timeCount();
            } else {
                $('#authCodeBtn').html("重新发送");
                timeLeft=60;
                $("#authCodeBtn").attr('disabled',false);
            }
        }, 1000);
    };
    //发送短信验证码
    $('#authCodeBtn').click(function () {
        $('.notice').css("display","none");
        $('#authCodeBtn').attr('disabled',true);
        var phoneLen = phone.val().length;
        if(phoneLen == 0){
            dangerNotice("手机号不能为空");
            $('#authCodeBtn').attr('disabled',false);
        } else {
            if(phone.hasClass("right")){
                $.ajax({
                    type:'post',
                    url:'/getCode',
                    dataType:'json',
                    data:{
                        phone:$('#phone').val(),
                        sign:"changePassword"
                    },
                    success:function (data) {
                        if(parseInt(data) == 1) {
                            successNotice("短信验证码发送成功");
                            timeCount();
                        }
                    },
                    error:function () {
                    }
                });
            } else {
                dangerNotice("手机号不正确");
                $('#authCodeBtn').attr('disabled',false);
            }
        }

    });

    //修改密码
    $('#changePasswordBtn').click(function () {
        $('.notice').css("display","none");
        if(phone.val().length === 0){
            dangerNotice("手机号不能为空");
        } else if (phone.hasClass("wrong")){
            dangerNotice("手机号不正确");
        } else if (authCode.val().length === 0){
            dangerNotice("验证码不能为空");
        } else if (password.val().length === 0){
            dangerNotice("新密码不能为空");
        } else if (surePassword.val().length === 0){
            dangerNotice("确认密码不能为空");
        } else{
            if (password.val() !== surePassword.val()){
                dangerNotice("确认密码不正确");
            } else {
                $.ajax({
                    type:'post',
                    url:'/changePassword',
                    dataType:'json',
                    data:{
                        phone:phone.val(),
                        authCode:authCode.val(),
                        newPassword:password.val()
                    },
                    success:function (data) {
                        if(data == "0"){
                            dangerNotice("验证码不正确")
                        }else if (data == "2"){
                            dangerNotice("手机号不存在")
                        }else if(data == "1"){
                            successNotice("密码修改成功");
                        }
                    },
                    error:function () {
                        alert("修改密码失败");
                    }
                })
            }
        }
    });

    //填充文章管理
    function putInArticleManagement(data) {
        var articleManagementTable = $('.articleManagementTable');
        articleManagementTable.empty();
        $.each(data['result'], function (index, obj) {
            articleManagementTable.append($('<tr id="' + obj['articleId'] + '"><td><a href="' + obj['articleUrl'] + '">' + obj['articleTitle'] + '</a></td><td>' + obj['articleDate'] + '</td><td>' + obj['articleCategories'] + '</td> <td>' + obj['articleAuthor'] + '</td><td><span class="am-badge am-badge-success">' + obj['articleHot'] + '</span></td>' +
                '<td>' +
                '<div class="am-dropdown" data-am-dropdown>' +
                '<button class="articleManagementBtn articleEditor am-btn am-btn-secondary">编辑</button>' +
                '<button class="articleDeleteBtn articleDelete am-btn am-btn-danger">删除</button>' +
                '</div>' +
                '</td>' +
                '</tr>'));
        });
        articleManagementTable.append($('<div class="my-row" id="page-father">' +
            '<div id="articleManagementPagination">' +
            '<ul class="am-pagination  am-pagination-centered">' +
            '</ul>' +
            '</div>' +
            '</div>'));

        $('.articleManagementBtn').click(function () {
            var $this = $(this);
            var id = $this.parent().parent().parent().attr("id").substring(0);
            window.location.replace("/article/update/" + id);
        });
        $('.articleDeleteBtn').click(function () {
            var $this = $(this);
            deleteArticleId = $this.parent().parent().parent().attr("id").substring(0);
            $('#deleteAlter').modal('open');
        })
    }
    //删除文章
    $('.sureArticleDeleteBtn').click(function () {
        $.ajax({
            type:'get',
            url:'/article/deleteArticle/'+deleteArticleId,
            dataType:'json',
            data:{

            },
            success:function (data) {
                if(data == 0){
                    dangerNotice("删除文章失败")
                } else {
                    successNotice("删除文章成功");
                    getArticleManagement(1);
                }
            },
            error:function () {
                alert("删除失败");
            }
        });
    })
    //获得文章管理文章
    function getArticleManagement(currentPage) {
        $.ajax({
            type:'get',
            url:'/findArticle/getArticleManagement',
            dataType:'json',
            data:{
                rows:10,
                pageNum:currentPage
            },
            success:function (data) {
                putInArticleManagement(data);
                scrollTo(0,0);//回到顶部

                //分页
                $("#articleManagementPagination").paging({
                    rows:data['pageInfo']['pageSize'],//每页显示条数
                    pageNum:data['pageInfo']['pageNum'],//当前所在页码
                    pages:data['pageInfo']['pages'],//总页数
                    total:data['pageInfo']['total'],//总记录数
                    callback:function(currentPage){
                        getArticleManagement(currentPage);
                    }
                });
            },
            error:function () {
                alert("获取文章信息失败");
            }
        });
    }

    //点击相册管理
    $('.leaveMessage').click(function () {
        $("#Images li img").height($("#Images li img").width());
    });
    //点击文章管理
    $('.privateWord').click(function () {
        getArticleManagement(1);
    });
    showHeadPortrait();
    getUserPersonalInfo();

